﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PaliTextReader2.Utilities;

namespace PaliTextReader2
{
    public partial class MainWnd : Form
    {
        private SelectBook dlg;
        
        public MainWnd()
        {
            InitializeComponent();
        }

        private void MainWnd_Load(object sender, EventArgs e)
        {
            if (Convert.ToInt32(m_objFilesTA.CheckColumnExists("ShowBold")) == 0)            
                m_objFilesTA.AddShowBoldColumn();

            if (Convert.ToInt32(m_objFilesTA.CheckColumnExists("ShowFont")) == 0)
                m_objFilesTA.AddShowFontColumn();

            dlg = new SelectBook();
            AppState.Instance().Deserialize();

            AppState objAppState = AppState.Instance();
            foreach (OpenBookInfo objOBI in objAppState.OpenBooks)
            {
                BookViewerControl objViewForm = new BookViewerControl(objOBI.FileID, objOBI.ScrollPos);
                OpenBookTab(objViewForm, objOBI.FileID);
            }
        }

        private void ctlOpenBook_Click(object sender, EventArgs e)
        {
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                BookViewerControl objViewForm = null;
                int FileID=-1;
                if(dlg.FileID!=-1)
                {
                    if (dlg.ScrollPos != -1)
                        objViewForm = new BookViewerControl(dlg.FileID, dlg.ScrollPos);
                    else
                        objViewForm = new BookViewerControl(dlg.FileID, dlg.PTSPage);

                    FileID = dlg.FileID;
                }
                else
                {
                    objViewForm = new BookViewerControl(dlg.NodeID);
                    ContentsDS.NodesDataTable objNodes = m_objNodesTA.GetNode(dlg.NodeID);
                    if (objNodes.Rows.Count > 0)
                        FileID = objNodes[0].FileID;
                }

                dlg.FileID = -1;

                OpenBookTab(objViewForm, FileID);
             
            }
        }

        public void OpenBookTab(BookViewerControl objViewForm, int FileID)
        {
            objViewForm.LookUp += new BookViewerControl.LookUpHandler(objViewForm_LookUp);

            objViewForm.Dock = DockStyle.Fill;

            ContentsDS.NodesDataTable objTopMostNodes = m_objNodesTA.GetFileName(FileID);

            TabPage tp = new TabPage();
            tp.Parent = ctlBookTabs;
            tp.ImageIndex = 0;
            if (objTopMostNodes.Rows.Count > 0)
            {
                tp.Text = objTopMostNodes[0].NodeTitle;
                tp.ToolTipText = Utilities.Utility.GetNodePath(objTopMostNodes[0].NodeID);
            }

            tp.Controls.Add(objViewForm);
            tp.Show();

            objViewForm.Show();
            ctlBookTabs.SelectedTab = tp;  
        }

        void objViewForm_LookUp(string message)
        {
            splitContainer1.Panel2Collapsed = false;
            ctlToggleDictionary.Checked = true;
            ctlDictionary.DoSearch(message);
        }

        private void ctlBookTabs_MouseClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < ctlBookTabs.TabCount; i++)
            {
                Rectangle r = ctlBookTabs.GetTabRect(i);
                r.Offset(2, 2);
                r.Width = 16;
                r.Height = 16;
                if (r.Contains(e.X, e.Y))
                {
                    string strPrompt = "Are you sure you want to close this book?";

                    if (ctlBookTabs.TabPages[i].Controls[0] is SearchControl)
                        strPrompt = "Are you sure you want to close this window?";

                    if (MessageBox.Show(strPrompt,
                        "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        BookViewerControl objBookViewer = ctlBookTabs.TabPages[i].Controls[0] as BookViewerControl;

                        if (objBookViewer!=null)
                        {
                            AppState.Instance().RecentBooks.Insert(0,
                                new BookmarkInfo(ctlBookTabs.TabPages[i].Text, 
                                    objBookViewer.FileID, objBookViewer.ScrollPos));

                            if (AppState.Instance().RecentBooks.Count > 50)
                                AppState.Instance().RecentBooks.RemoveAt(50);
                        }

                        ctlBookTabs.TabPages.RemoveAt(i);
                    }
                }
            }
        }

        private void ctlToggleDictionary_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel2Collapsed = !ctlToggleDictionary.Checked;
        }

        private void ctlQuickNav_Click(object sender, EventArgs e)
        {
            if (ctlBookTabs.SelectedTab != null && ctlBookTabs.SelectedTab.Controls[0] is BookViewerControl)
            {
                BookViewerControl objBookViewer = ctlBookTabs.SelectedTab.Controls[0] as BookViewerControl;
                if (objBookViewer != null)
                    objBookViewer.ShowQuickNav();
            }
        }

        private void MainWnd_FormClosed(object sender, FormClosedEventArgs e)
        {
            AppState objAppState = AppState.Instance();
            objAppState.OpenBooks.Clear();

            foreach (TabPage objTab in ctlBookTabs.TabPages)
            {
                if (objTab.Controls[0] is BookViewerControl)
                {
                    BookViewerControl objBookViewer = objTab.Controls[0] as BookViewerControl;
                    if (objBookViewer != null)
                        objBookViewer.SaveState(objAppState);
                }
            }

            AppState.Instance().Serialize();
        }

        private void ctlFind_Click(object sender, EventArgs e)
        {
            if (ctlBookTabs.SelectedTab != null)
            {
                if (ctlBookTabs.SelectedTab.Controls[0] is BookViewerControl)
                {
                    BookViewerControl objBookViewer = ctlBookTabs.SelectedTab.Controls[0] as BookViewerControl;
                    if (objBookViewer != null)
                        objBookViewer.ShowFindDlg();
                }

                if (ctlBookTabs.SelectedTab.Controls[0] is SearchControl)
                {
                    SearchControl objSearch = ctlBookTabs.SelectedTab.Controls[0] as SearchControl;
                    if (objSearch != null)
                        objSearch.ShowFindDlg();
                }
            }

        }

        private void ctlSearchBooks_Click(object sender, EventArgs e)
        {
            bool bOpen = false;

            foreach (TabPage objTab in ctlBookTabs.TabPages)
                if (objTab.Controls[0] is SearchControl)
                {
                    ctlBookTabs.SelectedTab = objTab;
                    bOpen = true;
                }

            if (!bOpen)
            {
                SearchControl ctlSearch = new SearchControl();
                ctlSearch.Dock = DockStyle.Fill;

                TabPage tp = new TabPage();
                tp.Parent = ctlBookTabs;
                tp.ImageIndex = 0;
                tp.Text = "Search";
                tp.Controls.Add(ctlSearch);
                tp.Show();

                ctlSearch.Show();
                ctlBookTabs.SelectedTab = tp;
            }
        }

        private void ctlExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ctlBookmarkPage_Click(object sender, EventArgs e)
        {
            if (ctlBookTabs.SelectedTab != null && ctlBookTabs.SelectedTab.Controls[0] is BookViewerControl)
            {
                BookViewerControl objBookViewer = ctlBookTabs.SelectedTab.Controls[0] as BookViewerControl;
                if (objBookViewer != null)
                {
                    BookmarkNameDlg dlgBookmarkName = new BookmarkNameDlg();
                    if (dlgBookmarkName.ShowDialog() == DialogResult.OK)
                    {
                        AppState objAppState = AppState.Instance();
                        objAppState.Bookmarks.Add(new BookmarkInfo(dlgBookmarkName.BookmarkName, 
                            objBookViewer.FileID, objBookViewer.ScrollPos));
                    }
                }
            }
        }

        private void ctlAbout_Click(object sender, EventArgs e)
        {
            AboutBox dlgAboutBox = new AboutBox();
            dlgAboutBox.ShowDialog();
        }

        private void ctlBookSettings_Click(object sender, EventArgs e)
        {
            if (ctlBookTabs.SelectedTab != null && ctlBookTabs.SelectedTab.Controls[0] is BookViewerControl)
            {
                BookViewerControl objBookViewer = ctlBookTabs.SelectedTab.Controls[0] as BookViewerControl;
                if (objBookViewer != null)
                {
                    BookSettingsDlg dlg = new BookSettingsDlg(objBookViewer.FileID);
                    dlg.ShowDialog();
                }
            }
        }
    }
}
